package cn.broadsense.web.controller.biz;

import cn.broadsense.biz.domain.ItemTag;
import cn.broadsense.biz.enums.FillTagEnum;
import cn.broadsense.biz.service.ItemTagService;
import cn.broadsense.biz.vo.FillTagVo;
import cn.broadsense.biz.vo.ItemTagVo;
import cn.broadsense.common.annotation.Anonymous;
import cn.broadsense.common.annotation.Log;
import cn.broadsense.common.core.controller.BaseController;
import cn.broadsense.common.core.domain.R;
import cn.broadsense.common.core.page.TableDataInfoResult;
import cn.broadsense.common.enums.BusinessType;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @author lijian
 * @description
 * @date 2024年09月12日 上午11:53
 */
@RestController
@Api(tags = "填充标签管理")
@RequestMapping("/item/tag")
@RequiredArgsConstructor
@Anonymous
public class ItemTagController extends BaseController {

    private final ItemTagService itemTagService;

    @Log(title = "填充标签管理-新增", businessType = BusinessType.INSERT)
    @ApiOperation(value = "新增填充标签")
    @PostMapping("/add")
    public R add(@Validated @RequestBody ItemTagVo vo) {
        String filedName = FillTagEnum.getFiledName(vo.getFieldNameDb());
        if (StrUtil.isEmpty(filedName)) {
            return R.fail("填充标签不存在");
        }
        ItemTag itemTag = new ItemTag();
        itemTag.setTagType(vo.getTagType());
        itemTag.setIsRequired(vo.getIsRequired());
        itemTag.setFieldNameDb(vo.getFieldNameDb());
        itemTag.setFieldName(filedName);
        itemTag.setRemark(vo.getRemark());
        itemTagService.save(itemTag);
        return R.ok();
    }

    @Log(title = "填充标签管理-编辑", businessType = BusinessType.UPDATE)
    @ApiOperation(value = "编辑填充标签")
    @PutMapping("/update/{id}")
    public R add(@Validated @RequestBody ItemTagVo vo, @PathVariable Long id) {
        ItemTag one = itemTagService.getById(id);
        if (one == null) {
            return R.fail("标签不存在");
        }
        String filedName = FillTagEnum.getFiledName(vo.getFieldNameDb());
        if (StrUtil.isEmpty(filedName)) {
            return R.fail("填充标签不存在");
        }
        itemTagService.lambdaUpdate()
                .set(ItemTag::getFieldName, filedName)
                .set(ItemTag::getTagType, vo.getTagType())
                .set(ItemTag::getIsRequired, vo.getIsRequired())
                .set(ItemTag::getRemark, vo.getRemark())
                .eq(ItemTag::getId, id)
                .update();
        return R.ok();
    }

    @ApiOperation(value = "详情")
    @GetMapping("/{id}")
    public R<ItemTag> getDetail(@PathVariable Long id) {
        ItemTag one = itemTagService.getById(id);
        if (one == null) {
            return R.fail("标签不存在");
        }
        one.setFieldName(FillTagEnum.getFiledName(one.getFieldNameDb()));
        return R.ok(one);
    }

    @ApiOperation(value = "删除")
    @DeleteMapping("/{id}")
    public R delete(@PathVariable Long id) {
        ItemTag one = itemTagService.getById(id);
        if (one == null) {
            return R.fail("标签不存在");
        }
        itemTagService.removeById(id);
        return R.ok();
    }

    @ApiOperation(value = "列表")
    @GetMapping("/list")
    public TableDataInfoResult<List<ItemTag>> list(@ApiParam(value = "填充标签名称") @RequestParam(required = false) String fieldName,
                                                      @ApiParam(value = "标签类型") @RequestParam(required = false) Integer tagType,
                                                      @RequestParam(required = false) @ApiParam(value = "开始时间 YYYY-MM-DD") String startTime,
                                                      @RequestParam(required = false) @ApiParam(value = "结束时间 YYYY-MM-DD") String endTime) {
        startPage();
        List<ItemTag> list = itemTagService.lambdaQuery()
                .like(StrUtil.isNotBlank(fieldName), ItemTag::getFieldName, fieldName)
                .eq(Objects.nonNull(tagType), ItemTag::getTagType, tagType)
                .between(StrUtil.isNotBlank(startTime) && StrUtil.isNotEmpty(endTime), ItemTag::getUpdateTime, startTime, endTime)
                .orderByDesc(ItemTag::getUpdateTime)
                .list();
        return getDataTableResult(list);
    }


    @GetMapping("/tagOption")
    @ApiOperation(value = "填充标签选项")
    public List<FillTagVo> tagOption(@ApiParam(value = "规则名称") String fieldName) {
        List<FillTagVo> result = CollectionUtil.newArrayList();
        for (FillTagEnum fillTagEnum : EnumSet.allOf(FillTagEnum.class)) {
            FillTagVo vo = new FillTagVo();
            vo.setFieldName(fillTagEnum.getFieldName());
            vo.setFieldNameDb(fillTagEnum.getFieldNameDb());
            result.add(vo);
        }
        if(StrUtil.isNotBlank(fieldName)){
            result = result.stream().filter(x -> x.getFieldName().contains(fieldName)).collect(Collectors.toList());
        }
        return result;
    }
}
